<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="入库单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleOrder">
              <a-input v-model="model.serial" placeholder="请输入入库单号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
              <j-dict-select-tag type="list" v-model="model.supplierId" dictCode="hf_supplier,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplier">
              <a-input type="list" v-model="model.supplierOrderSerial" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="源单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceSerial">
              <j-popup
                v-model="model.sourceSerial"
                field="sourceSerial"
                org-fields="id,serial,document_date,supplier_id,supplier_order_serial,source_type,source_id,source_serial,delivery_time,operator"
                dest-fields="id,serial,documentDate,supplierId,supplierSerial,sourceType,sourceId,sourceSerial,deliveryTime,operator"
                code="purchaseInputSourceSerial"
                :multi="false"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="源单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceType">
              <j-dict-select-tag type="list" v-model="model.sourceType" dictCode="hf_purchaseorder_source" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentDate">
              <j-date v-model="model.documentDate" placeholder="请输入单据日期" style="width: 100%"></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结算日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentTime">
              <j-date
                placeholder="请选择结算日期"
                v-model="model.paymentTime"
                :show-time="true"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="源单id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceId">
              <j-dict-select-tag type="list" v-model="model.sourceId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="红蓝单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRed">
              <j-dict-select-tag type="list" v-model="model.isRed" dictCode="hf_if_red" placeholder="请选择红蓝单" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-search-select-tag
                v-model="model.operator"
                title="制单"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <j-search-select-tag
                v-model="model.checker"
                title="审核员"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <a-input v-model="model.checkTime" placeholder="请输入审核日期" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import store from '@/store'
import dayjs from 'dayjs'

export default {
  name: 'HfPurchaseInputModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 1000,
      visible: false,
      isChecked: false,
      disableSubmit: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        isRed: 0,
        status: 0,
        isClosed: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        supplierId: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
        documentDate: [{ required: true, message: '单据日期不能为空', trigger: 'blur' }]
      },
      url: {
        add: '/purchase/hfPurchaseInput/add',
        edit: '/purchase/hfPurchaseInput/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault, false)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      if (this.disableSubmit) {
        this.handleCancel()
      } else {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            httpAction(httpurl, this.model, method)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.confirmLoading = false
                that.close()
              })
          } else {
            return false
          }
        })
      }
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.model.sourceId = row.id
      this.model.sourceSerial = row.serial
    }
  }
}
</script>
